package com.pignest.hotel.system;

import java.util.Collection;
import java.util.HashMap;

import com.pignest.hotel.annotation.Bean;
import com.pignest.hotel.beans.Log;
import com.pignest.hotel.beans.global.BaseBean;
import com.pignest.hotel.service.AdminService;

/**
 * 记录日志
 * */
public class L {
	
	private static HashMap<String, String> maps = new HashMap<String, String>();
	
	public static class Action {
		public static final String INSERT = "新增";
		public static final String UPDATE = "更新";
		public static final String DELETE = "删除";
		public static final String LOGIN  = "登录";
		public static final String LOGOUT = "退出";
		public static final String START  = "开始";
		public static final String PAUSE  = "暂停";
		public static final String STOP   = "停止";
		public static final String SYSTEM = "系统";
		public static final String ENABLE = "启用";
		public static final String DISABLE = "禁用";
		public static final String REPORT = "报表";
	}
	
	public static void log(String action) {
		Log l = new Log(action);
		service.insert(l);
	}
	
	public static void log(String action, String category) {
		Log l = new Log(action, category);
		service.insert(l);
	}
	
	public static void log(String action, String category, String description) {
		Log l = new Log(action, category, description);
		service.insert(l);
	}
	
	public static void log(String action, String category, String description, String parameter) {
		Log l = new Log(action, category, description, parameter);
		service.insert(l);
	}
	
	private static void log(String action, BaseBean bean) {
		log(action, getBeanName(bean), bean.toSimpleString(), bean.getId());
	}
	
	private static void log(String action, BaseBean bean, String desc) {
		log(action, getBeanName(bean), bean.toSimpleString(desc), bean.getId());
	}
	
	public static String getBeanName(Class<?> clz) {
		String clazz = clz.getName();
		String name = maps.get(clazz);
		if (name != null)
			return null;
		Bean a = clz.getAnnotation(Bean.class);
		if (a != null)
			name = a.value();
		else
			name = clz.getSimpleName();
		maps.put(clazz, name);
		return name;
	}
	
	public static String getBeanName(BaseBean bean) {
		return getBeanName(bean.getClass());
	}
	
	//////////////////////////////////////////////////////////////////////////////
	
	public static void insert(BaseBean bean) {
		log(L.Action.INSERT, bean);
	}
	
	public static void update(BaseBean bean) {
		log(L.Action.UPDATE, bean);
	}
	
	public static void delete(BaseBean bean) {
		log(L.Action.DELETE, bean);
	}
	
	public static void insert(BaseBean bean, String desc) {
		log(L.Action.INSERT, bean, desc);
	}
	
	public static void update(BaseBean bean, String desc) {
		log(L.Action.UPDATE, bean, desc);
	}
	
	public static void delete(BaseBean bean, String desc) {
		log(L.Action.DELETE, bean, desc);
	}
	
	public static void update(Collection<? extends BaseBean> beans) {
		if (beans == null || beans.isEmpty())
			return;
		if (beans.size() == 1)
			L.update(beans.iterator().next());
		else
			L.update(beans.iterator().next(), beans.size() + " records");
	}
	public static void update(Collection<? extends BaseBean> beans, String desc) {
		if (beans == null || beans.isEmpty())
			return;
		if (beans.size() == 1)
			L.update(beans.iterator().next(), desc);
		else
			L.update(beans.iterator().next(), beans.size() + " records. " + desc);
	}
	
	public static void enable(boolean enable, BaseBean bean) {
		log(enable ? L.Action.ENABLE : L.Action.DISABLE, bean);
	}
	
	public static void enable(boolean enable, BaseBean bean, String desc) {
		log(enable ? L.Action.ENABLE : L.Action.DISABLE, bean, desc);
	}
	
	public static void action(String action, BaseBean bean) {
		log(action, bean);
	}
	
	public static void action(String action, BaseBean bean, String desc) {
		log(action, bean, desc);
	}
	
//	public static void login() {
//		
//		Log log = new Log(Action.LOGIN, Category.USER);
//		service.insert(log);
//	}
//	public static void logout() {
//		Log log = new Log(Action.LOGOUT, Category.USER);
//		service.insert(log);
//	}
	
	/////////////////////////////////////////////////////////////////////////////////
	
	private static AdminService service;
	
	public static void setService(AdminService logService) {
		L.service = logService;
	}

	private L() {
	}
}
